package com.xhwl.logistics.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: luyi
 * @Description: mybatisplus 默认时间配置
 * @Date: Create in 9:42 2020/5/15
 */
@Component
public class DefaultMetaObjectHandler implements MetaObjectHandler {


    private static final Logger log = LoggerFactory.getLogger(DefaultMetaObjectHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        boolean hasSetterc = metaObject.hasSetter("createAt");
        boolean hasSetterced = metaObject.hasSetter("createdAt");
        Date now = new Date();
        if(hasSetterc){
            this.setFieldValByName("createAt", now, metaObject);
        }
        if(hasSetterced){
            this.setFieldValByName("createdAt", now, metaObject);
        }



        boolean hasSetteru = metaObject.hasSetter("updateAt");
        boolean hasSetterued = metaObject.hasSetter("updatedAt");

        if(hasSetteru){
            this.setFieldValByName("updateAt", now, metaObject);
        }
        if(hasSetterued){
            this.setFieldValByName("updatedAt", now, metaObject);
        }


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        boolean hasSetterue = metaObject.hasSetter("updateAt");
        if(hasSetterue){
            this.setFieldValByName("updateAt",  new Date(), metaObject);
        }
        boolean hasSetterued = metaObject.hasSetter("updatedAt");
        if(hasSetterued){
            this.setFieldValByName("updatedAt",  new Date(), metaObject);
        }




    }
}
